import axios from "axios";
import { Message, Loading } from "element-ui";
import Router from "@/router.js";

let loading;

const startLoading = () => {
  loading = Loading.service({
    lock: true,
    text: "加载中……",
    background: "rgba(0, 0, 0, 0.7)"
  });
};

const endLoading = () => {
  loading.close();
};

let needLoadingRequestCount = 0;
export function showFullScreenLoading() {
  if (needLoadingRequestCount === 0) {
    startLoading();
  }
  needLoadingRequestCount++;
}

export function tryHideFullScreenLoading() {
  if (needLoadingRequestCount <= 0) return;
  needLoadingRequestCount--;
  if (needLoadingRequestCount === 0) {
    endLoading();
  }
}

// 请求前做什么
axios.interceptors.request.use(
  config => {
    showFullScreenLoading();
    return config;
  },
  err => {
    Message.error({
      message: "请求超时"
    });
    return Promise.resolve(err);
  }
);

// 请求后做什么
axios.interceptors.response.use(
  data => {
    if (data.data.code == 401) {
      Message.error({
        message: data.data.msg
      });
      Router.push("/login");
    }
    if (data.status && data.status == 200 && data.data.status == "error") {
      Message.error({
        message: data.data.msg
      });
      return;
    }
    tryHideFullScreenLoading();
    return data;
  },
  err => {
    console.log(err);
    if (!err.response) {
      tryHideFullScreenLoading();
      return Message({
        message: "加载失败"
      });
    }
    if (err.response.status == 504 || err.response.status == 404) {
      Message.error({
        message: "服务器炸了"
      });
      tryHideFullScreenLoading();
    } else if (err.response.status == 403) {
      Message.error({
        message: "权限不足，请联系管理员!"
      });
      tryHideFullScreenLoading();
    } else {
      Message.error({
        message: "网络连接失败"
      });
      tryHideFullScreenLoading();
    }
    return Promise.resolve(err);
  }
);

/**
 * 封装GET请求
 * @param {*} url
 * @param {*} params
 * @return Promise
 */
export function getRequest(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        params: params
      })
      .then(response => {
        resolve(response.data);
      })
      .catch(err => {
        reject(err);
      });
  });
}

/**
 * 封装POST请求
 * @param {*} url
 * @param {*} data
 * @return Promise
 */
export function postRequest(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios
      .post(url, data, { credentials: true })
      .then(response => {
        resolve(response.data);
      })
      .catch(err => {
        reject(err);
      });
  });
}

/**
 * 封装PUT请求
 * @param {*} url
 * @param {*} data
 */
export function putRequest(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios
      .put(url, data, { credentials: true })
      .then(response => {
        resolve(response.data);
      })
      .catch(err => {
        reject(err);
      });
  });
}

/**
 * 封装DELETE请求
 * @param {*} url
 * @param {*} data
 */
export function deleteRequest(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios
      .delete(url, data, { credentials: true })
      .then(response => {
        resolve(response.data);
      })
      .catch(err => {
        reject(err);
      });
  });
}
